from datetime import datetime


def calculate_refund_premium(insurance_start, insurance_end, amendment_effective_date, total_premium):
    date_format = "%Y-%m-%d %H:%M:%S"

    # 将字符串格式的日期转换为datetime对象
    start_date = datetime.strptime(insurance_start, date_format)
    end_date = datetime.strptime(insurance_end, date_format)
    effective_date = datetime.strptime(amendment_effective_date, date_format)

    # 计算保期总天数
    policy_duration_days = (end_date - start_date).days + 1

    # 转换为月份（假设每个月30天）
    policy_duration_months = policy_duration_days / 30

    # 已了月份计算：((批改生效日期-保险起期)/30)，并根据规则调整
    days_passed = (effective_date - start_date).days + 1  # 加1确保包含起始日
    completed_months = days_passed / 30

    # 特殊情况处理：当已了月份为零点几时，默认按1个月处理
    if 0 < completed_months < 1:
        completed_months = 1
    else:
        completed_months = int(completed_months)  # 否则取整数部分

    # 确保不会出现负数或超过保期月份的情况
    completed_months = min(completed_months, int(policy_duration_months))

    # 计算退还保费
    if policy_duration_months > 0:
        refund_premium = total_premium - (total_premium / policy_duration_months * completed_months)
    else:
        refund_premium = total_premium  # 如果保期为0个月，则退还全部保费

    return round(refund_premium, 2)


# 示例使用
insurance_start = "2025-01-11 00:00:00"  # 保险起期
insurance_end = "2025-06-10 23:59:59"  # 保险止期
amendment_effective_date = "2025-01-12 23:59:59"  # 批改生效日期
total_premium = 5.0  # 总保费

refund = calculate_refund_premium(insurance_start, insurance_end, amendment_effective_date, total_premium)
print(f"退还保费: {refund}元")